#!/usr/bin/env python
# -*- coding: utf-8 -*-

'''eof
name:申请企业出现资产处置、担保代偿、垫款等违约情况
code:Ent_SpecialDefaultCnt
tableName:
columnName:
groups:准入>企业征信校验
dependencies:IC_DSJ_2D
type:常用指标
dataSourceType:在线指标
description:
eof'''

'''
ECRSummarySettled01:已结清信贷信息概要(资产处置、被剥离负债、垫款
Debtsdamf_Number:由资产管理公司处置的债务（笔数）
StrippedLiabilitie_Number:被剥离负债汇总（笔数）

ECRSummarySettled04:已结清信贷信息概要(垫款汇总、代偿)
Guarantee_Number:担保代偿或第三方代偿的债务（笔数）
PaidbackBalancesum_Number:垫款汇总（笔数）

ECRSummaryUnsettled01:未结清信贷信息概要(资产处置、欠息)
Debtsdamf_Number:由资产管理公司处置的债务（笔数）

ECRSummaryUnsettled04:未结清信贷信息概要(垫款汇总、代偿)
Guarantee_Number:担保代偿或第三方代偿的债务（笔数）
PaidbackBalancesum_Number:垫款汇总（笔数）

ECRPaidBadLoanInfo: 已结清不良、关注类贷款
Fiveclasscode：五级分类



'''

import sys
import json


reload(sys)
sys.setdefaultencoding('utf-8')


def FormatData(table_name):
    null_type_list = ['', None, 'null', '/', 'Null']
    try:
        if EC in null_type_list:
            return None
        if len(EC) == 0:
            return None
        ecr = json.loads(EC[0]["ecr"])[table_name]
        return ecr if isinstance(ecr, list) else None
    except:
        return None


def ConvertDataType(data_value, data_type):
    return_data = None
    if data_type == 'float':
        try:
            return_data = float(data_value) if float(data_value) >= 0 else None
        except:
            return_data = None
    elif data_type == 'None':
        return_data = 0 if data_value is None else data_value
    return return_data


def Ent_SpecrialDefaultCnt():
    try:
        null_type_list = ['', None, 'null', 'Null', 'NULL', '/', '--', '-']
        # 已结清信贷信息概要(垫款汇总、代偿)
        ECRSummarySettled01 = FormatData("ECRSummarySettled01")
        if ECRSummarySettled01 not in null_type_list and len(ECRSummarySettled01) >= 1:
            # 由资产管理公司处置的债务（笔数）
            Debtsdamf_Number = ConvertDataType(ECRSummarySettled01[0].get("Debtsdamf_Number"), 'float')
            # 被剥离负债汇总（笔数）
            StrippedLiabilitie_Number = ConvertDataType(ECRSummarySettled01[0].get("StrippedLiabilitie_Number"),'float')
            if Debtsdamf_Number is None and StrippedLiabilitie_Number is None:
                Factor1 = None
            else:
                Factor1 = ConvertDataType(Debtsdamf_Number, 'None') + ConvertDataType(StrippedLiabilitie_Number, 'None')
        else:
            Factor1 = None


        # 已结清信贷信息概要(垫款汇总、代偿)
        ECRSummarySettled04 = FormatData("ECRSummarySettled04")
        if ECRSummarySettled04 not in null_type_list and len(ECRSummarySettled04) >= 1:
            # 担保代偿或第三方代偿的债务（笔数）
            Guarantee_Number = ConvertDataType(ECRSummarySettled04[0].get("Guarantee_Number"), 'float')
            # 垫款汇总（笔数）
            PaidbackBalancesum_Number = ConvertDataType(ECRSummarySettled04[0].get("PaidbackBalancesum_Number"), 'float')
            if Guarantee_Number is None and PaidbackBalancesum_Number is None:
                Factor2 = None
            else:
                Factor2 = ConvertDataType(Guarantee_Number, 'None') + ConvertDataType(PaidbackBalancesum_Number, 'None')
        else:
            Factor2 = None


        # 未结清信贷信息概要(资产处置、欠息)
        ECRSummaryUnsettled01 = FormatData("ECRSummaryUnsettled01")
        if ECRSummaryUnsettled01 not in null_type_list and len(ECRSummaryUnsettled01) >= 1:
            # 由资产管理公司处置的债务（笔数）
            Factor3 = ConvertDataType(ECRSummaryUnsettled01[0].get("Debtsdamf_Number"), 'float')
        else:
            Factor3 = None
        # 未结清信贷信息概要(垫款汇总、代偿)
        ECRSummaryUnsettled04= FormatData("ECRSummaryUnsettled04")
        if ECRSummaryUnsettled04 not in null_type_list and len(ECRSummaryUnsettled04) >= 1:
            # 担保代偿或第三方代偿的债务（笔数）
            Guarantee_Number = ConvertDataType(ECRSummaryUnsettled04[0].get("Guarantee_Number"), 'float')
            # 垫款汇总（笔数）
            PaidbackBalancesum_Number = ConvertDataType(ECRSummaryUnsettled04[0].get("PaidbackBalancesum_Number"), 'float')
            if Guarantee_Number is None and PaidbackBalancesum_Number is None:
                Factor4 = None
            else:
                Factor4 = ConvertDataType(Guarantee_Number, 'None') + ConvertDataType(PaidbackBalancesum_Number, 'None')
        else:
            Factor4 = None


        #已结清不良、关注类贷款
        care_type = [u'次级', u'可疑', u'损失']
        non_type = [u'关注']
        Factor5 = []
        ECRPaidBadLoanInfo= FormatData("ECRPaidBadLoanInfo")
        if  ECRPaidBadLoanInfo not in null_type_list and len(ECRPaidBadLoanInfo)>=1:
            for i in ECRPaidBadLoanInfo:
                if i.has_key('Fiveclasscode') :
                    if  i['Fiveclasscode'] in care_type:
                        Factor5.append(float(1))
                    elif i['Fiveclasscode'] in non_type:
                        Factor5.append(float(0))
                    else:
                        Factor5.append(None)
                else:
                    Factor5.append(None)
        else:
            Factor5.append(None)
        # 若Factor5均为None,则返回缺失值,否则将None置为0
        Factor5 = filter(lambda x: x is not None, Factor5)
        Factor_five = sum(Factor5) if len(Factor5) != 0 else None
        # 指标值取5个值的和,若均为空则返回缺失值
        Factor= [Factor1, Factor2,Factor3,Factor4,Factor_five]
        overnum = filter(lambda x: x is not None, Factor)
        return sum(overnum) if len(overnum) != 0 else u'缺失值'

    except:
        return u'缺失值'





result = Ent_SpecrialDefaultCnt()

